package com.isoftstone.project.device.sensor.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.isoftstone.project.device.sensor.domain.USensor;
import com.isoftstone.project.device.sensor.service.IUSensorService;
import com.isoftstone.framework.controller.BaseController;
import com.isoftstone.framework.domain.Page;
import com.isoftstone.framework.domain.Tidings;
import com.isoftstone.common.utils.ExcelUtil;
import com.isoftstone.framework.domain.PageData;

/**
 * 传感器管理Controller
 *
 * @author guoX
 * @date 2021-02-02
 */
@Controller
@RequestMapping("/device/sensor")
public class USensorController extends BaseController {
    private String prefix = "device/sensor";

    @Autowired
    private IUSensorService uSensorService;

    @RequiresPermissions("device:sensor:view")
    @GetMapping()
    public String sensor() {
        return prefix + "/sensor_list";
    }

    /**
     * 查询传感器管理列表
     */
    @RequiresPermissions("device:sensor:list")
    @PostMapping("/uSensorlistPage")
    @ResponseBody
    public Object getuSensorlistPage(Page page) {
        PageData pd = this.getPageData();
        page.setPd(pd);
        List<USensor> list = uSensorService.getUSensorlistPage(page);
        return Tidings.success(page.getTotalResult(), list);
    }


    /**
     * 导出传感器管理列表
     */
    @RequiresPermissions("device:sensor:export")
    @PostMapping("/exportUSensor")
    @ResponseBody
    public Object exportUSensor(Page page) {
        PageData pd = this.getPageData();
        page.setPd(pd);
        page.setEntityOrField(false);
        List<USensor> list = uSensorService.getUSensorlistPage(page);
        ExcelUtil<USensor> util = new ExcelUtil<USensor>(USensor. class);
        return util.exportExcel(list, "sensor");
    }

    /**
     * 新增传感器管理
     */
    @GetMapping("/addUSensor")
    public String addUSensor() {
        return prefix + "/sensor_add";
    }


    /**
     * 新增保存传感器管理
     */
    @RequiresPermissions("device:sensor:add")
    @PostMapping("/addUSensor")
    @ResponseBody
    public Object insertUSensor(USensor uSensor) {
        uSensorService.insertUSensor(uSensor);
        return Tidings.success();

    }

    /**
     * 修改传感器管理
     */
    @GetMapping("/toEditUSensor")
    public String toEditUSensor(Long autoid, ModelMap mmap) {
        USensor uSensor =uSensorService.getUSensorById(autoid);
        mmap.put("uSensor", uSensor);
        return prefix + "/sensor_edit";
    }

    /**
     * 修改保存传感器管理
     */
    @RequiresPermissions("device:sensor:edit")
    @PostMapping("/updateUSensor")
    @ResponseBody
    public Object updateUSensor(USensor uSensor) {
        uSensorService.updateUSensor(uSensor);
        return Tidings.success();
    }

    /**
     * 删除传感器管理
     */
    @RequiresPermissions("device:sensor:remove")
    @PostMapping("/deleteUSensor")
    @ResponseBody
    public Object deleteUSensor(String autoid) {
        uSensorService.deleteUSensorByIds(autoid);
        return Tidings.success();
    }

}
